package com.example.baseutils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {
    /**
     * 得到当前时间
     *
     * @return String yyyy-MM-dd
     */
    public static String getCurrentShortTime() {
        SimpleDateFormat formatter = new SimpleDateFormat(CoreConfig.dateFormat1, Locale.CHINA);
        return formatter.format(new Date());
    }

    public static String getCurrentSecondsTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", Locale.CHINA);
        return formatter.format(new Date());
    }

    /**
     * 根据日期获取 星期 （2019-05-06 ——> 星期一）
     *
     * @param
     * @return
     */
    public static String getDateToWeek() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        String datetime = formatter.format(new Date());
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        Date date;
        try {
            date = f.parse(datetime);
            cal.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //一周的第几天
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 得到当前时间
     *
     * @return String yyyy-MM-dd-HH:mm:ss
     */
    public static String getCurrentShortTime(String fmt) {
        SimpleDateFormat formatter = new SimpleDateFormat(fmt, Locale.CHINA);
        return formatter.format(new Date());
    }

    /**
     * 计算两个时间的差
     *
     * @param startTimeStr
     * @param endTimeStr
     * @return
     */
    public static String getTimeDifference(String startTimeStr, String endTimeStr) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String differenceTime = "";
        try {
            differenceTime = getTimeDifference(formatter.parse(startTimeStr), formatter.parse(endTimeStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return differenceTime;
    }

    /**
     * 得到格式化时间
     *
     * @param timeStr
     */
    public static String getFormatterTime(String timeStr, String timeFormatter) {
        SimpleDateFormat formatter = new SimpleDateFormat(timeFormatter, Locale.CHINA);
        try {
            timeStr = formatter.format(formatter.parse(timeStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStr;
    }

    /**
     * 计算两个时间的差
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static String getTimeDifference(Date startTime, Date endTime) {
        long l = endTime.getTime() - startTime.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        // long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        if (day > 0) {
            return day + "天";
        }
        if (hour > 0) {
            return hour + "小时";
        }
        if (min > 30) {
            return min + "分钟";
        } else {
            return "刚刚";
        }
    }


    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(int time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = format.format(new Date(time * 1000L));
//	    System.out.println("日期格式---->" + times);
        return times;
    }


}
